﻿using System.Collections.Generic;
using System.Text;
using PacketRush.Core;

namespace PacketRush.Services.Commanding
{
    public class ClientCommandService : CommandService
    {
        private PacketWriter _PacketWriter = new PacketWriter();

        public Queue<Packet> _OutgoingPackets = new Queue<Packet>(8);

        public override void DeliverPacket(Packet packet)
        {
        }

        public override Packet CollectPacket()
        {
            if (_OutgoingPackets.Count > 0)
            {
                return _OutgoingPackets.Dequeue();
            }

            return null;
        }

        public void ExecuteCommand(string command, bool runAtServer)
        {
            if (runAtServer)
            {
                _PacketWriter.Reset();
                _PacketWriter.Write(command, Encoding.UTF8);
                _OutgoingPackets.Enqueue(_PacketWriter.ToPacket(PacketFlags.Reliable));
            }
            else
            {
                ExecuteCommand(command);
            }
        }
    }
}
